Why am I getting this error : "ExecuteReader: Connection property has not been initialized." [migrated]

Posted by Olga on Programmers See other posts from Programmers or by Olga
Published on 2012-06-11T15:10:31Z Indexed on 2012/06/11 16:47 UTC
Read the original article Hit count: 376

Filed under:

I'm trying to read .csv file to import its contents to SQL table

I'm getting error:

ExecuteReader: Connection property has not been initialized.

at the last line of this code:

Function ImportData(ByVal FU As FileUpload, ByVal filename As String, ByVal tablename As String) As Boolean
Try

    Dim xConnStr As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};dbq=" & Path.GetDirectoryName(Server.MapPath(filename)) & ";extensions=asc,csv,tab,txt;"

    ' create your excel connection object using the connection string
    Dim objXConn As New System.Data.Odbc.OdbcConnection(xConnStr.Trim())
    objXConn.Open()

    Dim objCommand As New OdbcCommand(String.Format("SELECT * FROM " & Path.GetFileName(Server.MapPath(filename)), objXConn))

    If objXConn.State = ConnectionState.Closed Then
        objXConn.Open()
    Else
        objXConn.Close()
        objXConn.Open()
    End If

    ' create a DataReader
    Dim dr As OdbcDataReader
    dr = objCommand.ExecuteReader()

© Programmers or respective owner

Related posts about ASP.NET